<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="demoTable layui-form">

            报修类型：
            <div class="layui-inline">
                <select name="comId"  id="typeId" lay-verify="required" lay-search="">
                    <option value="">选择报修类型</option>
                </select>
            </div>
            处理状态：
            <div class="layui-inline">
                <select name="status"  id="status" lay-verify="required" >
                    <option value="">选择状态</option>
                    <option value="0">待处理</option>
                    <option value="2">已处理</option>
                </select>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="add"> 我要报修 </button>
                </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">

            {{# if(d.status=='2' && d.commentId==null){ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="goPay">前往缴费</a>
            <a class="layui-btn layui-btn-xs layui-btn-primary data-count-delete" lay-event="addComment">去评价</a>

            {{# } else if(d.status == '2' && d.commentId != null){ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="goPay">前往缴费</a>
            {{# } }}

        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        table.render({
            elem: '#currentTableId',
            url: 'repair/queryRepairAll2',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {templet:'<div>{{d.type.name}}</div>',width: 100, title: '报修类型'},
                {field: 'remarks', width: 180, title: '报修内容'},
                {templet:'<div>{{d.owner.username}}</div>',width: 100, title: '报修人'},
                {field: 'phone', width: 150, title: '联系人电话'},
                {field: 'comDate', width: 150, title: '报修时间'},
                {field: 'hopeDate', width: 150, title: '期望修理时间'},

                {field: 'personnelName', width: 100, title: '处理人'},
                {field: 'personnelPhone', width: 100, title: '处理人联系电话'},
                {field: 'price', width: 80, title: '维修费用'},
                {field: 'status', width: 150, title: '处理状态',
                    templet: function(res){
                        if(res.status=='0'){
                            return '<span class="layui-badge layui-bg-red" >待处理</span>';
                        }else if (res.status == '1'){
                            return '<span class="layui-badge layui-bg-gray" >处理中</span>';
                        }else if (res.status == '2'){
                            return '<span class="layui-badge layui-bg-gray" >已处理,带缴费</span>';
                        }else if (res.status == '3'){
                            return '<span class="layui-badge layui-bg-gray" >已完成</span>';
                        }
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            // skin: 'line',
            id: 'testReload'
        });


        //获取费用类型 并渲染数据到select标签
        $.get("repair/queryAll",{},function (data) {
            var list=data;
            var select =document.getElementById("typeId");
            if(list!=null || list.size()>0){
                for(var c in list){
                    var option=document.createElement("option");
                    option.setAttribute("value",list[c].id);
                    option.innerText=list[c].name;
                    select.appendChild(option);
                }
            }
            form.render('select');
        },"json");


        var $ = layui.$, active = {
            reload: function(){
                var comId = $('#typeId');
                var status = $('#status');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        status:status.val(),
                        comId:comId.val()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
           if (obj.event === 'add') {  // 监听删除操作

               var content = miniPage.getHrefContent('page/other/addRepair.html');
               var index = openPage(content, "我要报修", '60%', '60%');
               $(window).on("resize", function () {
                   layer.full(index);
               });

           }
        });
        function setFormValues(data){
            console.log(data)
            form.val("updateSubmit",{
                "repairId":data.id,
                "repairPersonnelId":data.personnelId,
            });
        };


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'addComment') {

                var content = miniPage.getHrefContent('page/other/addComment.html');
                var index = openPage(content,"评价",'70%','70%');
                var data = obj.data;
                //渲染数据
                setFormValues(data);
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false
            } else if (obj.event === 'goPay') {
                layer.confirm('确定要前往缴费吗？', function (index) {
                    // console.log(data.id)
                    window.location.href = payUrl+"/payment/payRepair?id=" + data.id
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    deleteByIds($,"repair/deleteByIds",data.id,index)
                });
            }
            else if (obj.event === 'choose') {
                var content = miniPage.getHrefContent('page/business/repair/chooseRepairPersonnel.html');
                var index = openPage(content,"选择维修人员",'70%','70%');
                var data = obj.data;
                //渲染数据
                setFormValues(data);
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false
            }
        });



        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });



    });
</script>